package com.tyqx.hashchainbiz.controller.wx;

import hashchainutil.common.Dto;
import hashchainutil.common.DtoUtil;
import org.hyperledger.fabric.sdk.security.CryptoPrimitives;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Base64;
import java.util.List;

/**
 * @program: hashchain-spring
 * @description:
 * @author: Mr.Li
 * @create: 2020-10-30 21:21
 **/
@RestController
@RequestMapping("/wx/fileAuth")
@CrossOrigin
public class WxFileAuthController {

    @RequestMapping(value = "/sendFileAuth")
    @ResponseBody
    public Dto addJZTAttachmentBusiness(HttpServletRequest request) throws Exception {

        try {


           /* Gson gson = new GsonBuilder()
                    .setDateFormat("yyyy-MM-dd HH:mm:ss")
                    .create();
            WxCompany wxCompany = gson.fromJson(wxCompanyInfo, WxCompany.class);*/
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            List<MultipartFile> files = multipartRequest.getFiles("fileAuth");

            MultipartFile file = files.get(0);

            byte[] fileBytes=file.getBytes();
            CryptoPrimitives suite = new CryptoPrimitives();
            suite.init();
            byte[] hashBytes = suite.hash(fileBytes);

            String hashValue = Base64.getEncoder().encodeToString(hashBytes);

            return DtoUtil.returnSuccess("ok",hashValue);

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("发生学生档案错误","100001");
        }
    }
}
